﻿using Microsoft.Extensions.Options;
using Vin.Extension.Redis.Models;
using Vin.Tool.AspNetCore.Singleton;
using Vin.Tool.Core.BasicTypeCategory;

namespace Vin.Extension.Redis;

public static class VinRedisExtension
{
    public const string OptionKey = "VinCache:Redis";

    private const string Separator = ":";

    private static IOptionsMonitor<RedisOption>? _optionsMonitor;

    public static string? ConvertKey(this string? key)
    {
        if (string.IsNullOrEmpty(key)) return key;
        if (_optionsMonitor is null) _optionsMonitor = VinApp.GetRequiredService<IOptionsMonitor<RedisOption>>();
        return _optionsMonitor.CurrentValue.KeyRule switch
        {
            RedisKeyRule.CamelCase => key.ToCamelCase(Separator, true),
            RedisKeyRule.LowerCamelCase => key.ToCamelCase(Separator),
            RedisKeyRule.UnderLine => key.ToUnderLine(),
            RedisKeyRule.MiddleLine => key.ToMiddleLine(),
            _ => key
        };
    }
}